package com.itcanteen.medical.patient.service.impl;

import com.github.pagehelper.PageHelper;
import com.itcanteen.medical.common.constant.CommonStatus;
import com.itcanteen.medical.common.constant.Contstant;
import com.itcanteen.medical.common.vo.CommonResponse;
import com.itcanteen.medical.patient.entity.Doctor;
import com.itcanteen.medical.patient.entity.Patient;
import com.itcanteen.medical.patient.entity.Withdrawal;
import com.itcanteen.medical.patient.entity.Work;
import com.itcanteen.medical.patient.mapper.DoctorMapper;
import com.itcanteen.medical.patient.mapper.PatientMapper;
import com.itcanteen.medical.patient.mapper.WithdrawalMapper;
import com.itcanteen.medical.patient.mapper.WorkMapper;
import com.itcanteen.medical.patient.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.data.domain.PageRequest.of;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired
    @SuppressWarnings("all")
    private PatientMapper patientMapper;
    @Autowired
    private WithdrawalMapper withdrawalMapper;
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private WorkMapper workMapper;

    @Override
    public List<Patient> getAll(Patient patient) {
        if (patient.getPage() != null && patient.getRows() != null) {
            PageHelper.startPage(patient.getPage(), patient.getRows());
        }
        return patientMapper.selectAll();
    }


    @Override
    public List<Patient> findAll() {
        return patientMapper.selectAll();
    }


    @Override
    public Patient findByid(Patient patient) {
        return patientMapper.selectOne(patient);
    }

    //医生列表分页加模糊
    @Override
    public List<Doctor> getAllDoctor(Doctor doctor,Integer page,Integer rows) {
        if (page!=null&rows!=null){
            PageHelper.startPage(page,rows);
        }
        Example example = new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
         criteria.andEqualTo("level_id", doctor.getLevel_id());
        List<Doctor> doctors = doctorMapper.selectByExample(example);

        return doctors;
    }

    /*
     * 查询医生的工作时间
     * */
    @Override
    public List<Work> findByTime(Long doctorId) {
        Example example = new Example(Work.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId",doctorId);
        List<Work> works = workMapper.selectByExample(example);
        ArrayList<Work> list = new ArrayList<>();
        for (Work work : works) {
            Date date = new Date();
            Date endTime = work.getEndTime();

            long end = endTime.getTime();
            long dt = date.getTime();

            if (dt<end){
                list.add(work);
            }
        }
        return list;
    }

    /*
     * 根据用户选的就诊时间产看就诊时间是否过期
     * workId：就诊时间表id
     * */
    @Override
    public CommonResponse findByWork(Long workId) {
        Work work=null;

        if (workId!=null){
            work = workMapper.selectByPrimaryKey(workId);
        }

        if (work!=null){
            Date date = new Date();
            Date startTime = work.getStartTime();
            Date endTime = work.getEndTime();

            long start = startTime.getTime();
            long end = endTime.getTime();
            long dt = date.getTime();

            if (dt>=start&&dt<=end){
                return new CommonResponse(CommonStatus.VALID.getCode(),"挂号成功请即使就诊");
            }else if(dt<start){
                return new CommonResponse(CommonStatus.VALID.getCode(),"预约成功");
            }
        }

        return new CommonResponse(CommonStatus.INVALID.getCode(),"医生已经下班了");
    }

    /*
     * 所有医生列表
     * */
    @Override
    public List<Doctor> findByDoctorAll() {
        List<Doctor> doctors = doctorMapper.selectAll();
        return doctors;
    }
}
